﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.wlkc2011.Module.Default.model;
using Jxym.DBUtility;
using System.Reflection;
using System.Data.SqlClient;

namespace Jxym.wlkc2011.Module.Default.server.notice
{
    public partial class noticeAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                MsSqlDal<model.Notice> iUi = new MsSqlDal<model.Notice>();
                PropertyInfo[] fields = typeof(model.Notice).GetProperties();
                List<string> listName = new List<string>();
                List<string> listValue = new List<string>();
                MsSqlDal<model.User_Info> uinfo = new MsSqlDal<model.User_Info>();

                object identity = 0;
                int id = -1;
                int typeid = 1;
                int modelid = 0;

                if (Request.Params["modelid"] != null) modelid = Convert.ToInt32(Request.Params["modelid"]);

                string returnStr = "{'success':false,'msg':'参数错误'}";

                foreach (PropertyInfo property in fields)
                {
                    if (Request.Params[property.Name] != null)
                    {
                        listName.Add("@" + property.Name);
                        listValue.Add(Request.Params[property.Name]);
                    }
                }

                listName.Add("@AddUser");
                listValue.Add(Jxym.Common.Request.AdminID("user"));

                SqlParameter[] parameters = new SqlParameter[listName.Count];

                for (int i = 0; i < listName.Count; i++)
                {
                    parameters[i] = new SqlParameter(listName[i], listValue[i]);
                }

                if (Request.Params["ID"] != null)
                {
                    id = Convert.ToInt32(Request.Params["id"]);
                }
                if (Request.Params["TypeID"] != null)
                {
                    typeid = Convert.ToInt32(Request.Params["TypeID"]);
                }

                if (id < 0)
                {
                    Response.Write(returnStr);
                    return;
                }

                if (id > 0)
                {
                    Notice noctionmodel = iUi.GetModel("ID = " + id, null);
                    //判断该通知发布人是不是当前用户 和 当前用户权限
                    if (MyGet.GetPermissionsValidation(modelid, "U", Jxym.Common.Request.AdminID("user")) && Jxym.Common.Request.AdminID("user").Equals(noctionmodel.AddUser))
                    {
                        identity = iUi.Updata("TypeID = " + typeid + " and ID = " + id, parameters);
                        returnStr = "{'success':true,'msg':'更新完成'}";
                    }
                    else
                    {
                        returnStr = "{'success':false,'msg':'对不起，您没有编辑权限'}";
                    }
                }
                else
                {
                    if (MyGet.GetPermissionsValidation(modelid, "A", Jxym.Common.Request.AdminID("user")))
                    {
                        identity = iUi.Add(parameters);
                        returnStr = "{'success':true,'msg':'添加完成'}";
                    }
                    else
                    {
                        returnStr = "{'success':false,'msg':'对不起，您没有添加权限'}";
                    }
                }
                Response.Write(returnStr);
            }
        }
    }
}